﻿using Microsoft.Extensions.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Common
{

	public class LogHelper
	{

		static LogHelper()
		{
			var builder = new ConfigurationBuilder()
				.SetBasePath(Directory.GetCurrentDirectory())
				.AddJsonFile("appsettings.json");

			IConfigurationRoot config = builder.Build();
			connstr = config["ConnectionStrings:SqlConn_Log"];
			DBId = config["KingdeeCloud:dbId"];
		}


		//public static string connstr = "server=.;database=SuperKTV;uid=sa;pwd=123456";

		public static string connstr = null;
		//数据库链接对象
		private static SqlConnection Conn = null;
		//金蝶账套
		private static string DBId = null;

		//初始化数据库链接
		private static void InitConnection()
		{
			//如果连接对象不存在，创建连接
			if (Conn == null)
				Conn = new SqlConnection(connstr);
			//如果连接对象关闭，打开连接
			if (Conn.State == ConnectionState.Closed)
				Conn.Open();
			//如果连接中断，重启连接
			if (Conn.State == ConnectionState.Broken)
			{
				Conn.Close();
				Conn.Open();
			}
		}




		/// <summary>
		/// 日志写入
		/// </summary>
		/// <param name="WriteType">写入类型、平台</param>
		/// <param name="WriteID">单据ID</param>
		/// <param name="WriteBillNo">单据编号</param>
		/// <param name="WriteResult">写入状态</param>
		/// <param name="WriteDirection">写入方向</param>
		/// <param name="WriteData">传入报文</param>
		/// <param name="WriteMessage">返回值</param>
		/// <param name="WriteAbnormal">异常</param>
		/// <param name="FAcctID">账套</param>
		/// <returns></returns>
		public static int WriteLog(string WriteType, int WriteID, string WriteBillNo, int WriteResult, string WriteDirection, string WriteData, string WriteMessage, string WriteAbnormal, string FAcctID)
		{
			using (SqlConnection connection = new SqlConnection(connstr))
			{
				SqlCommand cmd = new SqlCommand($@"insert WEB_ApiResultLog
                                                   (FWriteType,FWriteID,FWriteBillNo,FWriteResult,FWriteDirection,FWriteData,FWriteMessage,FWriteAbnormal,FAcctID)
												   values
                                                   ('{WriteType}',{WriteID},'{WriteBillNo}',{WriteResult},'{WriteDirection}','{WriteData}','{WriteMessage}','{WriteAbnormal}','{DBId}')", connection);
				try
				{
					connection.Open();
					int rows = cmd.ExecuteNonQuery();
					return rows;
				}
				catch (System.Data.SqlClient.SqlException e)
				{
					throw e;
				}
				finally
				{
					cmd.Dispose();
					connection.Close();
				}
			}

		}

		public static int WriteCloudLog(string WriteType, int WriteID, string WriteBillNo, int WriteResult, string WriteDirection, string WriteData, string WriteMessage, string WriteAbnormal)
		{
			using (SqlConnection connection = new SqlConnection(connstr))
			{
				SqlCommand cmd = new SqlCommand($@"insert WEB_ApiCloudWriteLog
                                                   (FWriteType,FWriteID,FWriteBillNo,FWriteResult,FWriteDirection,FWriteData,FWriteMessage,FWriteAbnormal,FAcctID)
												   values
                                                   ('{WriteType}',{WriteID},'{WriteBillNo}',{WriteResult},'{WriteDirection}','{WriteData}','{WriteMessage}','{WriteAbnormal}','{DBId}')", connection);
				try
				{
					connection.Open();
					int rows = cmd.ExecuteNonQuery();
					return rows;
				}
				catch (System.Data.SqlClient.SqlException e)
				{
					throw e;
				}
				finally
				{
					cmd.Dispose();
					connection.Close();
				}
			}

		}



		public static DataTable ExecuteQuery(string strSQL, SqlParameter[] pas = null, CommandType commandType = CommandType.Text)
		{
			DataTable dt = new DataTable(); ;
			using (SqlConnection conn = new SqlConnection(connstr))
			{
				SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
				da.SelectCommand.CommandType = commandType;
				if (pas != null)
				{
					da.SelectCommand.Parameters.AddRange(pas);
				}
				da.Fill(dt);
			}
			return dt;
		}

	}

}